<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
class Article_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	/*
	 * 返回文章数量
	 */
	public function count_articles(){
		$query = $this->db->get('t_article');
		return $query->num_rows();
	}
	/*
	 *根据分页信息得到文章
	 */
	public function find_articles($per_page,$offset){//
		$this->db->select('t_article.id,t_class.name,t_article.title,t_article.content,t_article.date');
		$this->db->from('t_article');
		$this->db->join('t_class', 't_class.id = t_article.class','left');
		$this->db->limit($per_page, $offset);
		$this->db->order_by("date", "desc");
		$query = $this->db->get();
		return $query->result_array();
	}
	/*
	 * 根据ID得到文章信息
	 */
	public function load($id){
		$query = $this->db->get_where('t_article',array('id' => $id));
		if ($row = $query->row_array()){
			return $row;
		}
		return array();
	}
	/*
	 * 添加文章信息
	 */
	public function create($article){
		$this->db->insert('t_article', $article);
		return $this->db->insert_id();
	}
	/*
	 * 更新文章信息
	 */
	public function update($id,$article){
		$this->db->where('id', $id);
		$this->db->update('t_article', $article);
		return $this->db->affected_rows()>0;
	}
	/*
	 * 删除文章信息
	 */
	public function delete($id){
		$this->db->where('id', $id);
		$this->db->delete('t_article');
		return $this->db->affected_rows()>0;
	}
	/*
	 * 根据分类得到文章信息
	 */
	public function find_articles_by_cat($categoryid){
	    $this->db->where('class',$categoryid);
		$query = $this->db->get('t_article');
		return $query->result_array();
	}
}